package com.swagger.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2  // 开启swagger2
public class SwaggerConfig {
    @Bean
    public Docket docket1(Environment environment) {
        return new Docket(DocumentationType.SWAGGER_2).groupName("分组2");
    }
    // 配置Swagger得 Docket bean实例
    @Bean
    public Docket docket(Environment environment) {
        // 获取项目环境
        Profiles profiles = Profiles.of("dev");
        // 判断是否存在
        boolean isEnable = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 配置分组
                .groupName("分组1")
                // 配置是否启动 swagger
                .enable(isEnable)
                .select()
                // 配置扫描接口的方式
                //basePackage 扫描包
                // any  扫描所有
                // withClassAnnotation 扫描类上的注解，参数是一个注解得反射对象
                // withMethodAnnotation 扫描方法上得注解
                .apis(RequestHandlerSelectors.basePackage("com.swagger.swagger.controller"))
                //paths 过滤什么路径下得
//                .paths(PathSelectors.ant("/com.*"))
                .build();
    }

    //配置swagger信息
    private ApiInfo apiInfo() {
        return new ApiInfo(
                "Sagger 练习 title",
                "这是描述",
                "版本号：1.0",
                "url => localhost:8080/",
                new Contact("草莓sir", "", ""), // 作者信息
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}
